/** 
 * Description: 用户菜单顶部导航栏组件
 * Date: 2025/2/15
 */

<template>
  <div class="header-section">
    <div class="left-section">
      <div class="user-avatar">
        <image src="../../../image/index/user.png" alt="用户头像" />
      </div>
      <div class="username" @click.stop="toggleDropdownHandler">
        {{ displayUsername }}
      </div>
      <div class="dropdown-icon" @click.stop="toggleDropdownHandler">▼</div>
      
      <!-- 下拉菜单 -->
      <div v-if="showDropdown" class="dropdown-menu">
        <div class="user-info">
          <div class="logo">
            <image src="../../../image/icon_eilogo.png"></image>
          </div>
          <div class="user-details">
            <div class="user-name">{{ displayUsername }}</div>
            <div class="user-id">ID:{{ id || '未知' }}</div>
          </div>
        </div>

        <div class="menu-list">
          <div
            v-for="item in menuList"
            :key="item.id"
            @click="menuItemClickHandler(item.title)"
            class="menu-item">
            <div class="menu-icon">
              <image :src="item.imageUrl"></image>
            </div>
            <div class="menu-title">{{ item.title }}</div>
          </div>
        </div>
      </div>
    </div>
    
    <div class="right-section">
      <!-- 右侧设置按钮 - 只有sysadmin才能看到 -->
      <div v-if="username === 'sysadmin'" class="setting-button" @click="handleSettingClick">
        <image class="setting-icon" src="../../../image/userMenu/setting.png"></image>
        <span class="setting-text">设置</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HeaderSection',
  
  props: {
    username: {
      type: String,
      required: true
    },
    id: {
      type: String,
      default: ''
    },
    showDropdown: {
      type: Boolean,
      default: false
    },
    menuList: {
      type: Array,
      default: () => []
    }
  },
  
  computed: {
    /**
     * 显示用户名，如果username为空则显示默认值
     */
    displayUsername() {
      return this.username || '未登录用户';
    }
  },
  
  methods: {
    toggleDropdownHandler() {
      this.$emit('toggle-dropdown')
    },
    
    menuItemClickHandler(title) {
      this.$emit('menu-item-click', title)
    },
    
    /**
     * 处理设置按钮点击事件
     * 向父组件发送设置按钮点击事件
     */
    handleSettingClick() {
      this.$emit('setting-click')
    }
  }
}
</script>

<style scoped lang="scss">
.header-section {
  padding-top: 30px;
  padding-left: 40px;
  padding-right: 40px;
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;

  .left-section {
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;

    .user-avatar {
      image {
        width: 24px;
        height: 24px;
      }
    }

    .username {
      margin-left: 10px;
      cursor: pointer;
    }

    .dropdown-icon {
      margin-left: 5px;
      cursor: pointer;
      transition: transform 0.3s ease;
      
      &:hover {
        transform: rotate(180deg);
      }
    }

    .dropdown-menu {
      border-radius: 8px;
      min-width: 200px;
      position: absolute;
      top: 100%;
      left: 0;
      background-color: #f9f9f9;
      border: 1px solid #ddd;
      box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
      z-index: 1000;
      overflow: hidden;
      
      .user-info {
        display: flex;
        padding: 20px 15px;
        border-bottom: 1px solid #eee;

        .logo {
          image {
            width: 32px;
            height: 32px;
          }
        }

        .user-details {
          margin-left: 20px;
          display: flex;
          flex-direction: column;
          align-items: flex-start;
          color: #2c2c2c;

          .user-name {
            font-weight: bold;
            margin-bottom: 4px;
          }
          
          .user-id {
            font-size: 0.9em;
            color: #666;
          }
        }
      }

      .menu-list {
        padding: 10px 0;
        
        .menu-item {
          padding: 10px 20px;
          display: flex;
          align-items: center;
          color: #2c2c2c;
          transition: background-color 0.2s;
          
          &:hover {
            background-color: #f0f0f0;
          }
          
          .menu-icon {
            image {
              width: 20px;
              height: 20px;
              margin-right: 10px;
            }
          }
          
          .menu-title {
            font-size: 14px;
          }
        }
      }
    }
  }

  .right-section {
    display: flex;
    align-items: center;
    
    .setting-button {
      display: flex;
      align-items: center;
      cursor: pointer;
      padding: 8px 12px;
      border-radius: 4px;
      transition: background-color 0.2s ease;
      
      &:hover {
        background-color: rgba(255, 255, 255, 0.1);
      }
      
      &:active {
        background-color: rgba(255, 255, 255, 0.2);
      }
      
      .setting-icon {
        width: 24px;
        height: 24px;
      }
      
      .setting-text {
        margin-left: 10px;
        font-size: 14px;
      }
    }
  }
}
</style> 